﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeDomHelper.Types;
using Microsoft.VisualStudio.Modeling;
using devtm.AutoMapper.CustomCode;
using System.Reflection;
using devtm.Editor.TypeSelector;

namespace devtm.AutoMapper
{

    [CLSCompliant(false)]
    public static class Helpers
    {


        public static OutputWriter _output = new OutputWriter();

        public static string CleanRoot(string Namespace, string root)
        {

            if (Namespace.StartsWith(root))
                Namespace = Namespace.Substring(root.Length);

            return FixNamespace(Namespace).Trim();

        }

        public static string FixNamespace(string name)
        {

            if (string.IsNullOrEmpty(name))
                return string.Empty;

            if (name.StartsWith("."))
                name = name.Substring(1);

            if (name.EndsWith("."))
                name = name.Substring(0, name.Length - 1);

            return name.Trim();

        }

        private static System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("([A-Z]+)", System.Text.RegularExpressions.RegexOptions.None);
        public static string SplitMajuscule(string text)
        {
            return reg.Replace(text, " $1").Trim();
        }

        internal static string GetField(string text)
        {
            return "_" + GetXmlName(text).Trim();
        }

        internal static string GetXmlName(string text)
        {
            if (string.IsNullOrEmpty(text))
                return String.Empty;

            string l1 = text.Substring(0, 1).ToLower();
            string l2 = text.Substring(1);

            return (l1 + l2).Trim();
        }


        public static VisualStudio.ParsingSolution.Project FindProject(Store store, TypeExpression type)
        {

            string typeAssemblyName = type.AssemblyName;
            VisualStudio.ParsingSolution.Solution sln = new VisualStudio.ParsingSolution.Solution();
            foreach (VisualStudio.ParsingSolution.Project item in sln.GetItem<VisualStudio.ParsingSolution.Project>(true))
            {


                string assemblyName = item.AssemblyName;

                if (assemblyName == typeAssemblyName)
                    return item;

            }
            return null;

        }



        public static Type FindType(string typeName, Store store, HashSet<String> namespaces)
        {

            if (string.IsNullOrEmpty(typeName))
                return null;

            if (!typeName.Contains("["))
            {

                foreach (Type item in GetTypes(store, namespaces))
                    if (item.AssemblyQualifiedName == typeName)
                        return item;

                Type t = Type.GetType(typeName);

                if (t == null)
                    _output.WriteLine("can't resolve the type '{0}'", typeName);

                return t;

            }
            else
            {
                var e = (TypeExpression)typeName;
                return e.Evaluate(c => FindType(c, store, namespaces));
            }

        }


        public static IEnumerable<Type> GetTypes(Store store, HashSet<String> namespaces)
        {

            foreach (Type item in ContainerTypes.GetFromSolution<Object>(store, namespaces))
                yield return item;

            foreach (Type item in ContainerTypes.GetFromReference(store, namespaces))
                yield return item;

        }


        public static MemberInfo[] GetMembers(this Type ttt, string name)
        {

            MemberInfo[] r = ttt.GetMember(name);
            if (r == null || r.Length == 0)
            {
                if (ttt.BaseType != null && ttt.BaseType != typeof(Object))
                {
                    return GetMembers(ttt.BaseType, name);
                }
            }
            return r;
        }

    }

}
